package com.cn.unarmed.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.LinearLayout;


import com.cn.unarmed.activity.R;

import java.util.HashMap;

/**
 * 区域背景色,定义将北京某一竖条改变颜色
 */
public class ProgressBGView extends LinearLayout {
    public final String TAG = "ProgressBGView";
    public final int Rectangle = 1;
    public Context context;

    //progress max value
    public int limit = 10;
    //view size
    public int height = 100;
    public int width = 10;
    //default background color
    public int defaultColor;
    //draw view shape
    public int type = 0;

    //use to draw
    public Paint paintView;

    //color map use to draw background for data
    public HashMap<Integer, Integer> colorMap;
    //which key is object's type,and if not you want can return null;
    public HashMap<String, Object> data;

    //when value is true limit need change by data,false not do
    public boolean isCorrectionLimit = true;

    public ProgressBGView(Context context) {
        this(context, null);
    }

    public ProgressBGView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressBGView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.context = context;
        defaultColor = R.color.grey;
        paintView = new Paint();
        paintView.setStyle(Paint.Style.FILL);
        paintView.setAntiAlias(true);
        type = Rectangle;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getBGHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getBGWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public String getType() {
        if (type == Rectangle)
            return "rectangle";
        else
            return "null";
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setData(HashMap<String, Object> data) {
        this.data = data;
        invalidate();
    }

    public void setColor(HashMap<Integer, Integer> colorMap) {
        this.colorMap = colorMap;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if ((getMeasuredWidth() > 0 && getMeasuredHeight() > 0)) {
            if (getMeasuredWidth() != width || getMeasuredHeight() != height) {
                width = getMeasuredWidth();
                height = getMeasuredHeight();
                invalidate();
            }
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            if (data != null && data.get("array") != null) {
                int[] array = (int[]) data.get("array");
                if (null == array || array.length == 0) {
                    return;
                }
                limit = isCorrectionLimit ? array.length : limit;
                float shapeWidth = 0, shapeHeight = 0;
                float marginWidth = 0, marginHeight = 0;
                Path path;
                if (type == Rectangle) {
                    shapeHeight = height;
                    double d2 = width / (double) limit;
                    shapeWidth = (float) d2;
                    marginWidth = (width - shapeWidth * limit) / 2;
                    marginHeight = 0;
                }
                float startX = (int) marginWidth;
                float startY = (int) marginHeight;
                if (colorMap != null && colorMap.size() > 0) {
                    for (int i = 0; i < array.length; i++) {
                        paintView.setColor(context.getResources().getColor(colorMap.get(array[i])));
                        path = new Path();
                        path.moveTo(startX, startY);//base point
                        path.lineTo(startX + shapeWidth, startY);
                        path.lineTo(startX + shapeWidth, shapeHeight);
                        path.lineTo(startX, shapeHeight);
                        path.close();
                        canvas.drawPath(path, paintView);

                        startX += shapeWidth;
                    }
                } else {//default color
                    paintView.setColor(context.getResources().getColor(defaultColor));
                    for (int i = 0; i < array.length; i++) {
                        path = new Path();
                        path.moveTo(startX, startY);//base point
                        path.lineTo(startX + shapeWidth, startY);
                        path.lineTo(startX + shapeWidth, shapeHeight);
                        path.lineTo(startX, shapeHeight);
                        path.close();
                        canvas.drawPath(path, paintView);

                        startX += shapeWidth;
                    }
                }
            } else {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
